package com.powernode.shop.controller;

import com.powernode.shop.domain.Category;
import com.powernode.shop.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZHAOZHENYU
 * @since 2022/10/12
 */

@RestController
@RequestMapping("prod/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 1、分类列表
     * @return
     */
    @PreAuthorize("hasAuthority('prod:category:page')")
    @GetMapping("/table")
    public ResponseEntity<List<Category>> categoryPageList(){
        List<Category> categoryList = categoryService.getCategoryList();
        return ResponseEntity.ok(categoryList);
    }

    /**
     * 2、列表查出父分类
     * @return
     */
    @GetMapping("/listCategory")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> loadParentCategoryList(){
        List<Category> categoryList = categoryService.selectParentCategoryList();
        return ResponseEntity.ok(categoryList);
    }

    /**
     * 3、新增商品分类
     * @param category
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('prod:category:save')")
    public ResponseEntity<List<Category>> addCategory(@RequestBody Category category){
        categoryService.save(category);
        return ResponseEntity.ok().build();
    }


}
